import axios from 'axios'
import Vue from 'vue'
import router from '../router'

export function AxiosGet(url,parms){
    return new Promise((resolve,reject)=>{
        if(Vue.ls.get("sessionId")==undefined){
            console.log(Vue.ls.get("sessionId"));
            alert("登录失效")
            router.push('/')
            reject("登录失效")
        }
        axios({
            method:'get',
            url:'/api/'+url,
            data:parms,
            headers: {
                "Cookies":"JSESSIONID="+Vue.ls.get("sessionId"),
                'Access-Control-Allow-Origin': '*'
              },
        }).then(sucess=>{
            resolve(sucess.data)
        }).catch(error=>{
            reject(error)
        })
    })
}
export function AxiosPost(url,parms){
    return new Promise((resolve,reject)=>{
        if(Vue.ls.get("sessionId")==undefined){
            console.log(Vue.ls.get("sessionId"));
            alert("登录失效")
            router.push('/')
            reject("登录失效")
        }
        axios({
            method:'post',
            url:'/api/'+url,
            data:parms,
             headers: {
                "Cookies":"JSESSIONID="+Vue.ls.get("sessionId"),
                'Access-Control-Allow-Origin': '*'
              },
        }).then(sucess=>{
            resolve(sucess.data)
        }).catch(error=>{
            reject(error)
        })
            
    })
}
export function PostLoginSubmit(parms){
    return new Promise((resolve,reject)=>{
        axios({
            method:'post',
            url:'/api/login/submit',
            data:parms
        }).then(sucess=>{
            if(sucess.data.data==200){
                resolve(sucess.data.msg)
                Vue.ls.set("sessionId",sucess.data.msg,  30 * 60 * 1000)
            }
        }).catch(error=>{
            reject(error)
        })
            
    })
}
export function LoginOut(){
    return new Promise((resolve,reject)=>{
        axios.get('/login/logout').then(sucess=>{
            resolve(sucess.data)
        }).catch(error=>{
            reject(error)
        })
    })
}
export function GetLoingCode(parms){
    return new Promise((resolve,reject)=>{
        axios.get('/api/login/code/'+parms).then(sucess=>{
            resolve(sucess.data)
        }).catch(error=>{
            reject(error)
        })
    })
}
export function GetBingImage () {
   return new Promise((resolve,reject)=>{
    axios.get('/bing/HPImageArchive.aspx?format=js&idx=0&n=1&mkt=zh-CN').then(sucess=>{
        resolve(sucess.data)
    }).catch(error=>{
        reject(error)
    })
   })
    
}

